package com.yndsoft.jc.supplier.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.model.OrderModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.order.OnListChangeListener;
import com.yndsoft.jc.supplier.order.OrderDetailActivity;
import com.yndsoft.jc.supplier.order.entry.EntryDetailActivity;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerAdapter;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerHolder;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerListener;

import java.util.List;

public class OrderAdapter extends RecyclerAdapter<OrderModel> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    private String entryOrSign;
    private OnListChangeListener listener;

    public void setOnListChangeListener(OnListChangeListener listener) {
        this.listener = listener;
    }

    public OrderAdapter(Context context) {
        this(context, null);
    }

    public OrderAdapter(Context context, String entryOrSign) {
        super(context, null, R.layout.item_order);
        this.entryOrSign = entryOrSign;
    }

    @Override
    public void convert(RecyclerHolder holder, OrderModel model) {
        holder.setTvText(R.id.order_time, model.getCreatetime());
        holder.setTvText(R.id.order_number, "订单号: " + model.getId());
        Button orderOther = holder.findViewById(R.id.order_other);
        if (entryOrSign != null) {
            orderOther.setVisibility(View.VISIBLE);
            orderOther.setBackgroundResource(R.drawable.shape_supplier_orange_line);
            orderOther.setTextColor(0xFFEF7E0D);
            if (entryOrSign.equals(Constants.ENTRY)) {
                orderOther.setText("录入结果");
            } else if (entryOrSign.equals(Constants.SIGN)) {
                orderOther.setText("审核");
            }
        } else {
            if (6 == model.getState()) {
                orderOther.setText("待收货");
                orderOther.setVisibility(View.VISIBLE);
            } else {
                orderOther.setVisibility(View.GONE);
            }
        }

        List<OrderModel.DetsBean> dets = model.getDets();
        if (dets != null && dets.size() > 0) {
            RecyclerView orderProject = holder.findViewById(R.id.order_project);
            orderProject.setLayoutManager(new LinearLayoutManager(mContext));
            orderProject.setAdapter(new ProjectAdapter(mContext, dets));
        }

        holder.setOnClickListener(this, R.id.order_detail, R.id.order_other);
    }

    @Override
    public void onClick(View v, final int position, RecyclerHolder holder) {
        switch (v.getId()) {
            case R.id.order_detail:
                OrderDetailActivity.start(mContext, mData.get(position).getId(), null);
                break;
            case R.id.order_other:
                if (entryOrSign != null) {
                    if (entryOrSign.equals(Constants.ENTRY)) {
                        EntryDetailActivity.start(mContext, mData.get(position).getId());
                    } else if (entryOrSign.equals(Constants.SIGN)) {
                        OrderDetailActivity.start(mContext, mData.get(position).getId(), Constants.SIGN);
                    }
                } else {
                    if (6 == mData.get(position).getState()) {
                        mHttpUtil.orderWait(new ProgressSubscriber(mContext) {
                            @Override
                            public void next(ReturnMsg returnMsg) {
                                if (1 == returnMsg.getCode()) {
                                    if (listener != null) {
                                        listener.onListChange();
                                    }
                                    remove(position);
                                    ToastUtil.show(mContext, "收货成功！");
                                } else {
                                    ToastUtil.show(mContext, "收货失败！");
                                }
                            }
                        }, "get", mData.get(position).getId(), mSession.getSid());
                    }
                }
                break;
        }
    }
}
